package com.teemo.dp.flyweight.demo2;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Teemo
 */
public class PieceFactory {
    private Map<String, Piece> pieceMap = new HashMap<>();
    public Piece getPiece(String color) {
        if (pieceMap.containsKey(color)) {
            return pieceMap.get(color);
        } else {
            if (color.equals("black")) {
                BlackPiece blackPiece = new BlackPiece();
                pieceMap.put(color, blackPiece);
                return blackPiece;
            } else if (color.equals("white")) {
                WhitePiece whitePiece = new WhitePiece();
                pieceMap.put(color, whitePiece);
                return whitePiece;
            }
            return null;
        }
    }
}
